// VBMenuHtml.cpp
// www.vbmcgi.org

#ifndef __VBHTMLMENU__
#define __VBHTMLMENU__

#include <fstream>
#include "VBMcgi/VBMcgi.h"
#include "VBMcgi/VBMenuHtml.h"

using namespace br::com::sbVB::VBLib;
using namespace std;


br::com::sbVB::VBMcgi::VBMenuHtml::VBMenuHtml() 
{
	m_color = "#000000";
	m_size = "+1";
	m_bold = true;
	m_braket = true;
	m_horizontal = false;
	m_italic = false;
	m_underline = false;
	m_style = 0;
	m_javaScriptFileName = "VBF_rollover.js";
}

// sets style and paramenters
void br::com::sbVB::VBMcgi::VBMenuHtml::setStyle(VBString style, char tok) 
{
	m_style  = atoi(style.strtok(tok,0));
	m_color  = style.strtok(tok,1);
	m_size   = style.strtok(tok,2);
	m_bold   = (style.strtok(tok,3) == VBString("true"));
	m_braket = (style.strtok(tok,4) == VBString("true"));
	m_horizontal = (style.strtok(tok,5) == VBString("true"));
	m_italic = (style.strtok(tok,6) == VBString("true"));  //***
	m_underline = (style.strtok(tok,7) == VBString("true")); //***
}

void br::com::sbVB::VBMcgi::VBMenuHtml::menuFromFile(const char *fileName) 
{
	VBString menu, link, str;
	VBString imageName, imageOn, imageOff; //***
	ifstream file;
	file.open(fileName);
	bool fileExist = (file != 0);
	file.close();
	file.open(fileName);
	if (!fileExist || !file) 
	{
		cout << "File not open in menuFromFile method of VBMenuHtml class" << endl;
		return;
	}

	file >> str; // read first line of file
	char commentTok = str[0]; // first line must be a comment, first char of first line defines the comment char
	bool styleNotSet = true;
	bool menuTokNotSet = true;
	bool menuTitleNotSet = true;
	char menuTok;
	while (true) 
	{
		file >> str;
		if (file.eof()) break;
		if (str[0] == commentTok) continue; // ignore comment lines
		if (menuTokNotSet) 
		{
			menuTok = str[0];
			menuTokNotSet = false;
			continue;
		}
		if (styleNotSet && !menuTokNotSet) 
		{
			setStyle(str,menuTok);
			styleNotSet = false;
			continue;
		}
		if (menuTitleNotSet && !styleNotSet && !menuTokNotSet) 
		{
			cout << str << endl; // send title of menu to console
			menuTitleNotSet = false;
			continue;
		}

        // styles 3 and 4 require javascript functions to exist.
        // the code below outputs javascript code to inside the html
		if (m_style == 3 || m_style == 4) 
		{
            br::com::sbVB::VBMcgi::VBMcgi local_out;
            local_out.out(m_javaScriptFileName);
		}

		menu = str.strtok(menuTok,0);
		link = str.strtok(menuTok,1);
		imageName = str.strtok(menuTok,2);
		imageOff = str.strtok(menuTok,3);
		imageOn = str.strtok(menuTok,4);
		if (strlen(menu) > 0) 
			item(menu, link, imageName, imageOff, imageOn);
	}
}


void br::com::sbVB::VBMcgi::VBMenuHtml::item(VBString str, 
	VBString link, VBString imageName, VBString imageOff, VBString imageOn) 
{
	VBString a;
	switch (m_style) {
	case 0: // [ <a href="index.chtml?s_p=down_tut.html"><font color="#000000" size="+1">Download & tutorial</font></a> ]<br>
		if (m_braket) a += "[ ";
		a += "<a href=\"" + link + "\">";
		a += "<font color=\"" + m_color + "\" size=\"" + m_size + "\">";
		if (m_bold) a += "<b>";
		if (m_italic) a += "<i>"; //***
		if (m_underline) a += "<u>"; //***
		a += str;
		if (m_underline) a += "</u>"; //***
		if (m_italic) a += "</i>"; //***
		if (m_bold) a += "</b>";
		a += "</font>";
		a += "</a>";
		if (m_braket) a += " ]";
		if (m_horizontal) a += " ";
		else a += "<br>";
		cout << a << endl;
		break;

	case 1: // <nobr>[&nbsp;<a href="projetos.html">Projetos</a>&nbsp;]</nobr> 
		a += "<nobr>";
		if (m_braket) a += "[&nbsp;";
		a += "<a href=\"" + link + "\">";
		if (m_bold) a += "<b>";
		if (m_italic) a += "<i>"; //***
		if (m_underline) a += "<u>"; //***
		a += str;
		if (m_underline) a += "</u>"; //***
		if (m_italic) a += "</i>"; //***
		if (m_bold) a += "</b>";
		a += "</a>";
		if (m_braket) a += "&nbsp;]";
		a += "</nobr>";
		if (!m_horizontal) a += "<br>";
		cout << a << endl;
		break;

	//***
	// Menu with a static image: <a href="link"><img name="imageName" src="path/image_file" border="0"></a>
	case 2:
		if (m_braket) a += "[ ";
		a += "<a href=\"" + link + "\">";
		a += "<img name=\"" + imageName + "\" src\"" + imageOff +"\" border=\"0\">";
		a += "</a>";
		if (m_braket) a += " ]";
		if (m_horizontal) a += " ";
		else a += "<br>";
		cout << a << endl;
		break;

	//***
	// Menu with a rollover image.
	// <a href="link" onMouseOut="VBF_swapImgRestore()" onMouseOver="VBF_swapImage('Image1','','imageOn',1)">
    // <img name="imageName" border="0" src="imageOff"></a>
	case 3:
		if (m_braket) a += "[ ";
		a += "<a href=\"" + link + "\" onMouseOut=\"VBF_swapImgRestore()\" onMouseOver=\"VBF_swapImage(\'"
           + imageName + "\',\'\',\'" + imageOn + "\',1)\">";
		a += "<img name=\"" + imageName + "\" border=\"0\" src\"" + imageOff +"\">";
		a += "</a>";
		if (m_braket) a += " ]";
		if (m_horizontal) a += " ";
		else a += "<br>";
		cout << a << endl;
		break;

	//***
	// Menu with a text that changes the color when the mouse pass over it.
	case 4:
		cout << "Style not yet defined!<br>" << endl;
		break;

	default:
		cout << "Style not defined<br>" << endl;
	}
}

#endif // __VBHTMLMENU__

////////////////////////////////////////////////////////////////////////////////////
// Example code
////////////////////////////////////////////////////////////////////////////////////

#if 0
void menu(vbmcgi & cgi, void *p) {
	VBMenuHtml m;
	m.item("Download","index.chtml?s_p=download.html");
	m.item("History","index.chtml?s_p=history.html");
	m.item("Tutorial","index.chtml?s_p=tutorial.html");
	m.item("Complete reference","index.chtml?s_p=complete_reference.html");
	m.item("Email list","index.chtml?s_p=email_list.html");
	// m.item("Credits","index.chtml?s_p=credits.html");
}


void menuTutorial(vbmcgi & cgi, void *p) {
	cout << "<font color=\"#000066\" size=\"+1\">Tutorial examples:</font><br>" << endl;
	VBMenuHtml m;
	// m.setHorizontal(true);
	m.setColor("#0000aa");
	m.setBold(false);
	m.item("Hello CGI","index.chtml?s_p=hello_cgi.html");
	m.item("Multiply table","index.chtml?s_p=multiply_table.html");
	m.item("Hello VBMcgi","index.chtml?s_p=hello_vbmcgi.html");
	m.item("Multiply table with parameter","index.chtml?s_p=multiply_table_with_parameter.html");
	m.item("String parameters","index.chtml?s_p=string_parameters.html");
	m.item("External HTML file","index.chtml?s_p=external_html_file.html");
	m.item("formDecode adds variables","index.chtml?s_p=formDecode_adds_variables.html");
	m.item("Complete form reference","index.chtml?s_p=complete_form_reference.html");
	m.item("Calling functions","index.chtml?s_p=calling_functions.html");
	m.item("Calling functions with parameters","index.chtml?s_p=calling_functions_with_parameters.html");
	m.item("Cookies","index.chtml?s_p=cookies.html");
	m.item("Login to web using cookies","index.chtml?s_p=login_using_cookies.html");
}


void main () {
	vbmcgi cgi;
	cgi.addFunction(menu);
	cgi.addFunction(menuTutorial);
	cgi.formDecode();
	VBString link = cgi.getVarContent("s_p");
	cgi.removeBySource("s_p");
	// cout << "DEBUG:" << link << endl;
	if (link == VBString("")) {
		link = "index_original.html";
		VBPageCount myPageCount("index");
		cgi.addBySource("s_indexCount",myPageCount.getCount());
	}
	cgi.out(link);
}
#endif

